<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <div>{{name}}</div>
        <component-a :name="'ruijie'"></component-a>
        <component-b :name="'startnet'"></component-b>
    </div>    
</body>
<script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script>
<script>
    var car = new Vue();
    Vue.component('component-a',{
        props:['name'],
        data(){
            return {
                username:this.name
            }
        },
        mounted(){
            var that = this;
            car.$on('aData',function(msg){
                console.log('a');
                console.log(msg);
                that.username = msg;
            });
        },
        template:'<div @click="handleClick">my name is {{username}}</div>',
        methods:{
            handleClick(){
                car.$emit('aData',this.username);
                this.username = this.username === 'ruijie' ? 'startnet' : 'ruijie';
            }
        }
    });
    Vue.component('component-b',{
        props:['name'],
        data(){
            return {
                username:this.name
            }
        },
        mounted(){
            var that = this;
            car.$on('aData',function(msg){
                console.log('b');
                console.log(msg);
                that.username = msg;
            });
        },
        template:'<div @click="handleClick">my name is {{username}}</div>',
        methods:{
            handleClick(){
                car.$emit('aData',this.username);
                this.username = this.username === 'ruijie' ? 'startnet' : 'ruijie';
            }
        }
    });
    var app = new Vue({
        el:"#app",
        data(){
            return {
                name:"ruijie"
            }
        }
    })
</script>
</html>